package com.sleepapp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sleepapp.entity.Product;
import com.sleepapp.dao.ProductDao;
import com.sleepapp.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sleepapp.vo.PageQueryInfo;
import com.sleepapp.vo.PageQueryParamVo;
import com.sleepapp.vo.ProductQueryAllVo;
import com.sleepapp.vo.UProductQueryAllVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author 
 * @since 2025-03-13
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductDao, Product> implements ProductService {

    @Override
    public PageQueryInfo<List<ProductQueryAllVo>> getAll(PageQueryParamVo pageQueryParamVo) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(pageQueryParamVo.getKeyword()), Product::getName, pageQueryParamVo.getKeyword());

        IPage<Product> page = new Page<>(pageQueryParamVo.getPageNum(), pageQueryParamVo.getPageSize());

        this.page(page, queryWrapper);

        List<ProductQueryAllVo> productQueryAllVos = BeanUtil.copyToList(page.getRecords(), ProductQueryAllVo.class);

        return PageQueryInfo.of(productQueryAllVos, page.getTotal());
    }

    @Override
    public PageQueryInfo<List<UProductQueryAllVo>> getAllByUser(PageQueryParamVo pageQueryParamVo) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(pageQueryParamVo.getKeyword()), Product::getName, pageQueryParamVo.getKeyword());
        queryWrapper.eq(Product::getStatus, "AVAILABLE");

        IPage<Product> page = new Page<>(pageQueryParamVo.getPageNum(), pageQueryParamVo.getPageSize());

        this.page(page, queryWrapper);

        List<UProductQueryAllVo> uProductQueryAllVos = BeanUtil.copyToList(page.getRecords(), UProductQueryAllVo.class);
        return PageQueryInfo.of(uProductQueryAllVos, page.getTotal());
    }
}
